﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace NextCore
{
    public class NCore
    {
    }
    public enum MouseKey
    {
        Left,Right,Middle,Four,Five,All,MainThree,Null
    }
    public class NextApp : OpenTK.GameWindow
    {
        public static States AppStates = new States();
        public static NextLog Log = new NextLog();
        public NextApp(string app, int w, int h, bool full)
            : base(w, h, OpenTK.Graphics.GraphicsMode.Default, app, full ? GameWindowFlags.Fullscreen : GameWindowFlags.Default,DisplayDevice.Default,4,1,OpenTK.Graphics.GraphicsContextFlags.Default)
        {
            Log.Log(LogType.Core, "App succesfully initiated.", "NextCore", this as object,DateTime.Now);
            Log.ToConsole();
            MouseDown += new EventHandler<OpenTK.Input.MouseButtonEventArgs>(NextApp_MouseDown);
            MouseMove += new EventHandler<OpenTK.Input.MouseMoveEventArgs>(NextApp_MouseMove);
            MouseUp += new EventHandler<OpenTK.Input.MouseButtonEventArgs>(NextApp_MouseUp);
            Log.Log(LogType.System,"Running","",this as object,DateTime.Now);
            Log.ToConsole();

        }

        void NextApp_MouseUp(object sender, OpenTK.Input.MouseButtonEventArgs e)
        {

            switch (e.Button)
            {
                case OpenTK.Input.MouseButton.Button1:
                    OnMouseUp(e.Position, MouseKey.Left);
                    break;
                case OpenTK.Input.MouseButton.Button2:
                    OnMouseUp(e.Position, MouseKey.Right);
                    break;
                case OpenTK.Input.MouseButton.Button3:
                    OnMouseUp(e.Position, MouseKey.Middle);
                    break;
            }
        }

        void NextApp_MouseMove(object sender, OpenTK.Input.MouseMoveEventArgs e)
        {
     
            OnMouse(e.Position, e.XDelta, e.YDelta);
        }

        void NextApp_MouseDown(object sender, OpenTK.Input.MouseButtonEventArgs e)
        {
            switch (e.Button)
            {
                case OpenTK.Input.MouseButton.Left:
                    OnMouseDown(e.Position, MouseKey.Left);
                    break;
                case OpenTK.Input.MouseButton.Right:
                    OnMouseDown(e.Position, MouseKey.Right);
                    break;
                case OpenTK.Input.MouseButton.Middle:
                    OnMouseDown(e.Position,MouseKey.Middle);
                    break;
            }
        }
        public virtual void SetupGL()
        {

        }
        public virtual void OnMouseUp(System.Drawing.Point pos, MouseKey key)
        {

        }
        public virtual void OnMouse(System.Drawing.Point pos,int xd,int yd)
        {

        }
        public virtual void OnMouseDown(System.Drawing.Point pos, MouseKey key)
        {

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            CreateStates();
            Create();
            SetupGL();
            ResizeApp();
            Start();
            InitModules();
        }
        public virtual void InitModules()
        {

        }
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.ClearDepth(0.0f);
       
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            AppStates.RenderStates();
            Render();
            SwapBuffers();
        }
        protected override void OnResize(EventArgs e)
        {
  
            ResizeApp();
        }
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            AppStates.UpdateStates();
            Update();
        }
        public void AddState(NextState state)
        {
            AppStates.Add(state);
        }
        public void StartState(string id)
        {
            AppStates.Run(id);
        }
        public virtual void Start()
        {

        }
        public void StopState(string id)
        {
            AppStates.Stop(id);

        }
        public void StopAllStates()
        {
            AppStates.StopAll();
        }
        public virtual void CreateStates()
        {
        }
        public virtual void Create()
        {
        }
        public virtual void Update()
        {

        }
        public virtual void Render()
        {

        }
        public virtual void ResizeApp()
        {
        }
    }
}
